using UnityEngine;
using System.Collections.Generic;

[CreateAssetMenu(fileName = "InventoryData", menuName = "Inventory/InventoryData")]
public class InventoryData_SO : ScriptableObject
{
    /// <summary>
    /// 背包中所有格子的链表
    /// </summary>
    public List<InventoryItem> inventoryList = new List<InventoryItem>();

    public void AddItem(ItemData_SO item, int amount)
    {
        bool found = false;
        // 可以堆叠才去寻找同类物品
        if (item.stackable)
        {
            foreach (var inventoryItem in inventoryList)
            {
                if (inventoryItem.itemData == item)
                {
                    found = true;
                    // 堆叠数量增加
                    inventoryItem.inventoryAmount += amount;
                    break;
                }
            }
        }
        // 不可堆叠或未找到同类物品时
        if (!found)
        {
            for (int i = 0; i < inventoryList.Count; i++)
            {
                if (inventoryList[i].itemData == null)
                {
                    inventoryList[i].itemData = item;
                    inventoryList[i].inventoryAmount = amount;
                    break;
                }
            }
        }
    }
}
/// <summary>
/// 每个物品格子的数据
/// </summary>
[System.Serializable]
public class InventoryItem
{
    public ItemData_SO itemData;
    public int inventoryAmount;
}